package com.fyl.leetcode.binarySearch;

/**
 * @author:fyl
 * @date 2021/5/12 20:12
 * @Modified By:
 * @Modified Date:
 * @Description:
 * 给你一个排序后的字符列表 letters ，列表中只包含小写英文字母。另给出一个目标字母 target，请你寻找在这一有序列表里比目标字母大的最小字母。
 *
 * 在比较时，字母是依序循环出现的。举个例子：
 *
 * 如果目标字母 target = 'z' 并且字符列表为letters = ['a', 'b']，则答案返回'a'
 * 输入:
 * letters = ["c", "f", "j"]
 * target = "a"
 * 输出: "c"
 */
public class NextGreatestLetter {
    public char nextGreatestLetter(char[] letters, char target) {
        int low = 0;
        int high = letters.length - 1;
        while (low <= high) {
            int mid = low + ((high - low) >> 1);
            if (letters[mid] <= target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }

        }
        return low < letters.length ? letters[low] : letters[0];
    }
}
